home *** CD-ROM | disk | FTP | other *** search
- Path: morgan.cnu.edu!dlabell
- From: dlabell@pcs.cnu.edu (Daniel LaBell)
- Newsgroups: comp.lang.c++
- Subject: Re: C++ meta-problem
- Date: 08 Apr 1996 15:56:24 GMT
- Organization: A poorly-installed InterNetNews site
- Message-ID: <DLABELL.96Apr8115624@resolute.pcs.cnu.edu>
- References: <4jmtlg$smm@baskerville.CS.Arizona.EDU>
- NNTP-Posting-Host: resolute.pcs.cnu.edu
- In-reply-to: kdb@CS.Arizona.EDU's message of 31 Mar 1996 14:32:32 -0700
-
- In article <4jmtlg$smm@baskerville.CS.Arizona.EDU> kdb@CS.Arizona.EDU (Koen De Bosschere) writes:
-
- ) From: kdb@CS.Arizona.EDU (Koen De Bosschere)
- ) Newsgroups: comp.lang.c++
- ) Date: 31 Mar 1996 14:32:32 -0700
- )
- ) In order to prevent to write an iterator for every operation
- ) I want to perform on a list, I tried to write a kind of
- ) meta-iterator that would apply a particular method to
- ) all listnodes. For some reason, I cannot execute a
- ) function variable on an object. Does anyone has an
- ) idea how I can solve this problem?
- )
- [...]
- ) void iterate(testclass *root, void (testclass::*f)())
- ) {
- ) testclass *p = root;
- ) while (p) {
- ) p->f(); // <--- here the compiler generates an error (see below)
- ) p = p->getnext();
- ) }
- ) }
- [...]
-
- I have just done something somewhat like this myself, but I can't fully
- see what exactly what you're trying to do with 'p->f();' but I think the
- concept is similiar.
-
- (here's the part of the code that I think is relevant)
-
- void binSrchTree::inorder( void ( *proccessFunc) (class record a))
- {
- if ( root == NULL )
- return ;
- inorder ( root, proccessFunc );
- }
-
- void binSrchTree::inorder( binSrchTreeNode* n,
- void (*proccessFunc) (class record a))
- {
-
- if ( n->leftChild != NULL )
- inorder( n->leftChild, proccessFunc );
- (*proccessFunc) (n->data);
- if ( n->rightChild != NULL )
- inorder( n->rightChild, proccessFunc );
- }
-
- void PrintRecAndCompute ( record a )
- {
- Count++;
- AddToAverages( a );
- cout.flags( ios::left );
- cout << setw(25) << a.name << setprecision(4) << a.grades[0]
- << TAB << setprecision(4) << a.grades[1]
- << TAB << setprecision(4) << a.grades[2]
- << TAB << setprecision(4) << a.grades[3] << endl;
- }
-
- I'd call it with a line like this:
- Tree1.inorder(PrintRecAndCompute);
- --
- Daniel LaBell
-